package com.aiti.lulian.controller;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.base.core.page.PageResult;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.gxmh.dto.GxmhAppealParamDto;
import com.aiti.lulian.gxmh.dto.GxmhAppealQueryParamDto;
import com.aiti.lulian.gxmh.service.GxmhAppealService;
import com.aiti.lulian.gxmh.vo.GxmhAppealListItemVo;
import com.aiti.lulian.gxmh.vo.GxmhAppealVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * description: 工信门户--提诉求
 * auth fcb
 * date 2025-06-12 16:40
 **/
@RestController
@RequestMapping("/gxmhAppeal")
@Api(value = "工信门户--提诉求")
@Slf4j
public class GxmhAppealController extends WebController {

    @Autowired
    private GxmhAppealService gxmhAppealService;

    @PostMapping("/queryAppealList")
    @ApiOperation(value = "查询提诉求部门列表")
    public Message queryAppealList(@RequestBody GxmhAppealQueryParamDto queryDto) {
        try {
            PageResult<GxmhAppealVo> gxmhAppealVoPageResult = gxmhAppealService.queryAppealList(queryDto);
            return Success(gxmhAppealVoPageResult);
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Fail(e.getMessage());
            }
            log.error("查询提诉求列表异常:{}", ExceptionUtil.stacktraceToString(e));
            return Fail("接口异常：" + e.getMessage());
        }
    }

    //---------------------------------------------------管理平台--------------------------------------------------

    @PostMapping("/saveAppeal")
    @ApiOperation(value = "保存提诉求")
    public Message saveAppeal(@RequestBody GxmhAppealParamDto dto) {
        try {
            gxmhAppealService.saveAppeal(dto, findUserId());
            return Success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Fail(e.getMessage());
            }
            log.error("保存提诉求异常:{}", ExceptionUtil.stacktraceToString(e));
            return Fail("接口异常：" + e.getMessage());
        }
    }

    @PostMapping("/updateAppeal")
    @ApiOperation(value = "更新提诉求")
    public Message updateAppeal(@RequestBody GxmhAppealParamDto dto) {
        try {
            gxmhAppealService.updateAppeal(dto, findUserId());
            return Success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Fail(e.getMessage());
            }
            log.error("更新提诉求异常:{}", ExceptionUtil.stacktraceToString(e));
            return Fail("接口异常：" + e.getMessage());
        }
    }

    @PostMapping("/removeAppeal")
    @ApiOperation(value = "删除提诉求")
    public Message removeAppeal(@RequestBody List<String> baseIdList) {
        try {
            gxmhAppealService.removeAppreal(baseIdList);
            return Success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Fail(e.getMessage());
            }
            log.error("删除提诉求异常:{}", ExceptionUtil.stacktraceToString(e));
            return Fail("接口异常：" + e.getMessage());
        }
    }

    @PostMapping("/queryAppealListForManage")
    @ApiOperation(value = "管理系统--查询提诉求部门列表")
    public Message queryAppealListForManage(@RequestBody GxmhAppealQueryParamDto queryDto) {
        try {
            if(StrUtil.isEmpty(queryDto.getUserId())) {
                queryDto.setUserId(findUserId());
            }else {
                return Fail("用户标识不能为空");
            }
            PageResult<GxmhAppealVo> gxmhAppealVoPageResult = gxmhAppealService.queryAppealListForManage(queryDto);
            return Success(gxmhAppealVoPageResult);
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Fail(e.getMessage());
            }
            log.error("管理系统--查询提诉求部门列表:{}", ExceptionUtil.stacktraceToString(e));
            return Fail("接口异常：" + e.getMessage());
        }
    }

    @GetMapping("/queryAppealById")
    @ApiOperation(value = "查询提诉求部门详情")
    public Message queryAppealById(@RequestParam String baseId) {
        try {
            GxmhAppealListItemVo gxmhAppealListItemVo = gxmhAppealService.queryAppealById(baseId);
            return Success(gxmhAppealListItemVo);
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Fail(e.getMessage());
            }
            log.error("查询提诉求部门详情:{}", ExceptionUtil.stacktraceToString(e));
            return Fail("接口异常：" + e.getMessage());
        }
    }


}
